package com.zhaiweijia.eurekagateway.filter;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

//@Order(1)
@Slf4j
//@Component

public class MyGateWayFilter extends AbstractGatewayFilterFactory {


    public MyGateWayFilter() {
    }

    private static final String BENGIN_TIME = "bengin_time";

    @Override
    public GatewayFilter apply(Object config) {
        return (exchange, chain) -> {
            exchange.getAttributes().put(BENGIN_TIME,System.currentTimeMillis());
            return chain.filter(exchange).then(Mono.fromRunnable(()->{
                Long beginTime = exchange.getAttribute(BENGIN_TIME);
                log.info("请求时间为:{} 毫秒",System.currentTimeMillis()-beginTime);
            }));
        };
    }
}
